﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using Models;

namespace DAL
{
    public class IndexPicInfoDAL:IIndexPicInfo
    {
        public const string SQL_GET_INDEX_PIC= @"select * from IndexPicInfo ORDER BY PicOrder DESC";
        public DataBase DB = new DataBase();

        public List<IndexPicInfo> GetAllIndexPicInfo()
        {
            List<IndexPicInfo> list = new List<IndexPicInfo>();
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(SQL_GET_INDEX_PIC);
            while (reader.Read())
            {
                IndexPicInfo indexpicinfo = new IndexPicInfo();
                ReadtoIndexPicInfo(reader, indexpicinfo);
                list.Add(indexpicinfo);
            }
            DB.CloseDB();
            return list;
        }

        public void ReadtoIndexPicInfo(SqlDataReader reader, IndexPicInfo indexpicinfo)
        {
            try
            { indexpicinfo.IndexPicID = Int32.Parse(reader["IndexPicID"].ToString()); }
            catch
            { }
            indexpicinfo.PicURL = reader["PicURL"] is DBNull ? "" : reader["PicURL"].ToString();
            indexpicinfo.PicLink = reader["PicLink"] is DBNull ? "" : reader["PicLink"].ToString();
            indexpicinfo.PicOrder = reader["PicOrder"] is DBNull ? 0 : (int)reader["PicOrder"];
            indexpicinfo.PicTitle = reader["PicTitle"] is DBNull ? "" : reader["PicTitle"].ToString();
        }
        public int InsertIndexPicInfo(IndexPicInfo pic)
        {
            int isSuccess = -1;
            string sql = "INSERT INTO IndexPicInfo([PicURL],[PicOrder],[PicTitle],[PicLink])  VALUES('" + pic.PicURL + "'," + pic.PicOrder + ",'" + pic.PicTitle + "','" + pic.PicLink + "')";
            isSuccess = DB.UpdateRecords(sql);
            return isSuccess;
        }
        public int UpdateIndexPicInfo(IndexPicInfo pic)
        {
            string sql = @"UPDATE IndexPicInfo SET 
                        [PicURL] = '" + pic.PicURL + @"',
                        [PicOrder] = " + pic.PicOrder + @",
                        [PicTitle] = '" + pic.PicTitle + @"',
                        [PicLink] = '" + pic.PicLink + @"'                                           
                        WHERE IndexPicID = " + pic.IndexPicID;
            return DB.UpdateRecords(sql);
        }
        public int DeleteIndexPicInfo(IndexPicInfo pic)
        {
            int isSuccess = -1;
            string sql = " DELETE FROM IndexPicInfo WHERE IndexPicID = '" + pic.IndexPicID + "'";
            isSuccess = DB.UpdateRecords(sql);
            return isSuccess;
        }

        public IndexPicInfo GetIndexPicInfoFromUrl(string PicUrl)
        {
            string sql = "SELECT * FROM IndexPicInfo WHERE PicURL = '" + PicUrl + "'";
            IndexPicInfo indexpicinfo = new IndexPicInfo();
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(sql);
            while (reader.Read())
            {
                ReadtoIndexPicInfo(reader, indexpicinfo);   
            }
            DB.CloseDB();
            return indexpicinfo;
        }
    }
}
